package com.example.demo.controller;

import com.example.demo.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api")
public class ImageController {
    @Autowired
    private ImageService imageService;
    
    @PostMapping("/saveImagePath")
    public String saveImagePath(@RequestBody List<String> imagePathList) {
        try {
            imageService.saveImagePath(imagePathList);
        } catch (Exception e) {
            return "Error occurred while saving image paths: " + e.getMessage();
        }
        return "OK";
    }

    @GetMapping("/getRandomImage")
    public String getRandomImage() {
        try {
            String imagePath = imageService.getRandomImage();
            if (imagePath == null) {
                return "No available image found";
            }
            return imagePath;
        } catch (Exception e) {
            return "Error occurred while getting random image: " + e.getMessage();
        }
    }
}